class Patch

Attributes

area[R]
num_bands[R]
resources[R]

Public Class Methods

new(resources, conf) click to toggle source
# File lib/patch.rb, line 4
def initialize(resources, conf)
  stealables = [:area,:max_bands]
  stealables.each{|s|self.instance_variable_set("@#{s.to_s}".to_sym, conf[s.to_s])}
  @resources = resources.map{|type,cc| Object.const_get(type).new(cc, area)}

  @num_bands = 0
end

Public Instance Methods

depleted?() click to toggle source
# File lib/patch.rb, line 37
def depleted?
  @resources.all? { |r| r.depleted? }
end
do_time_step() click to toggle source
# File lib/patch.rb, line 33
def do_time_step
  @resources.each { |r| r.grow }
end
join() click to toggle source

serves a join attempt; returns success status

# File lib/patch.rb, line 13
def join
  if joinable?
    @num_bands += 1
    return true
  end
  false # Patch full, no room for more
end
joinable?() click to toggle source
# File lib/patch.rb, line 21
def joinable?
  @num_bands < @max_bands or @max_bands == 0
end
leave() click to toggle source
# File lib/patch.rb, line 25
def leave
  if @num_bands <= 0
    warn "Error: a band attempted to leave a patch with no bands"
    return
  end
  @num_bands -= 1
end
to_s() click to toggle source
# File lib/patch.rb, line 48
def to_s
  resources = "[" << @resources.map(&:to_s).reduce { |m, o| "#{m}, #{o}" } << "]" # Try that in Java
  "Patch (id:#{object_id}) { Bands: #{@num_inhabitants}/#{@max_bands} | Resources:#{resources} }"
end
viable?(resource) click to toggle source

does it have a viable population for the specified resource type?

# File lib/patch.rb, line 42
def viable?(resource)
  found = @resources.find { |r| r.name == resource }
  return found.viable? if found
  false
end